<?php if (!defined ( 'BASEPATH' )) exit ('No direct script access allowed');

/**
* 购物车
*/
class M_Cart extends CI_Model
{
    const TBL_SHOPPING_CARTS = 'shopping_carts';
    const TBL_SHOPPING_CART_ATTRIBUTES = 'shopping_cart_attributes';
    
	public function __construct(){
		parent::__construct();
	}

	/**
	 * 插入购物车
	 * @param  array  $items ['product_id','quantity','options=>array()']
	 * @return [int] 购物车id
	 */
	public function insert($items = array()) {
	    $this->db->trans_start();
	    
		$this->db->set('customer_id', $this->session->userdata('customer_id'));
		$this->db->set('product_id', $items['product_id']);
		$this->db->set('quantity', $items['quantity']);
		$this->db->set('add_date', 'now()', false);
		$this->db->insert(self::TBL_SHOPPING_CARTS);
		
		$shopping_cart_id = $this->db->insert_id();

		//插入商品属性
		if(isset($items['options']) && $shopping_cart_id){
			self::insert_attributes($shopping_cart_id, $items['options']);
		}
        
		$this->db->trans_complete();
		
		return $shopping_cart_id;
	}
	
	public function insert_attributes($shopping_cart_id, $options){
    	foreach ($options as $key => $value) {
			$data = array('shopping_cart_id'	=>	(int)$shopping_cart_id,
						  'option_id'			=>	$key,
						  'option_value_id'	    =>	$value);
			$this->db->insert(self::TBL_SHOPPING_CART_ATTRIBUTES,$data);
		}
	}

	public function update($shopping_cart_id, $items){
		self::update_quantity($shopping_cart_id, $items['quantity']);

		//判断是否要更新商品属性
		if (isset($items['options']) && is_array($items['options'])) {
			if(isset($items['options']['shopping_cart_attribute_id'])){
			    self::update_attribute($items['options']['shopping_cart_attribute_id'], $items['options']);
			}else{
			    self::delete_attributes($shopping_cart_id);
			    self::insert_attributes($shopping_cart_id, $items['options']);
			}
		}
	}
	
	public function update_quantity($shopping_cart_id, $quantity){
	    $this->db->set('quantity', (int)$quantity);
	    $this->db->where('shopping_cart_id', (int)$shopping_cart_id);
	    $this->db->update(self::TBL_SHOPPING_CARTS);
	    return $this->db->affected_rows();
	}
	
	public function update_attribute($shopping_cart_attribute_id, $options){
	    $this->db->set('option_id', $options['option_id']);
	    $this->db->set('option_value_id', $options['option_value_id']);
	    $this->db->where('shopping_cart_attribute_id', (int)$shopping_cart_attribute_id);
	    $this->db->update(self::TBL_SHOPPING_CART_ATTRIBUTES);
	}

	public function delete($shopping_cart_id) {
	    $this->db->trans_start();
	    
	    $this->db->where('shopping_cart_id', (int)$shopping_cart_id);
	    $this->db->where('customer_id', (int)$this->session->userdata('customer_id'));
		$this->db->delete(self::TBL_SHOPPING_CARTS);
		
		if($this->db->affected_rows() == 1){
    	    self::delete_attributes($shopping_cart_id);
		}
		
		$this->db->trans_complete();
	}
	
	public function delete_attributes($shopping_cart_id){
	    $this->db->where('shopping_cart_id', (int)$shopping_cart_id);
	    $this->db->delete(self::TBL_SHOPPING_CART_ATTRIBUTES);
	}

	private function is_customer_cart($shopping_cart_id){
	    $this->db->where('shopping_cart_id', (int)$shopping_cart_id);
	    $this->db->where('customer_id', (int)$this->session->userdata('customer_id'));
		$query = $this->db->get_where(self::TBL_SHOPPING_CARTS);
		
		return $query->num_rows() > 0 ? TRUE : FALSE;
	}
	
	/**
	 * 获取购物车数据
	 * @return array
	 */
	public function get_shopping_carts(){
	    $data = array();
	    $sql = 'SELECT
                	sc.shopping_cart_id,
	                sc.product_id,
                	pd.product_name,
                	pd.product_short_name,
                	sc.quantity
                FROM
                	shopping_carts sc,
                	product_descriptions pd
                WHERE
                	sc.product_id = pd.product_id
                AND pd.language_id = '.$this->session->userdata('language_id').'
                AND sc.customer_id = '.$this->session->userdata('customer_id');
	    $result = $this->db->query($sql);
	    $data = $result->result_array();
	    
	    foreach ($result->result_array() as $key => $value){
	        $sql = 'SELECT
                    	sca.shopping_cart_id,
                    	sca.option_id,
                    	f1.description as option_name,
                    	sca.option_value_id,
                    	f2.description as option_value_name
                    FROM
                    	shopping_cart_attributes sca,
                    	product_options po,
                    	product_option_values pov,
                    	fnd_descriptions f1,
                    	fnd_descriptions f2
                    WHERE
                    	sca.option_id = po.option_id
                    AND sca.option_value_id = pov.option_value_id
                    AND po.option_name_id = f1.description_id
                    AND pov.option_value_name_id = f2.description_id
                    AND f1.language_id = '.$this->session->userdata('language_id').'
                    AND f2.language_id = '.$this->session->userdata('language_id').'
                    AND sca.shopping_cart_id = '.$value['shopping_cart_id'].'
                    ORDER BY po.sort';
	        $result2 = $this->db->query($sql);
	        $data[$key]['options'] = $result2->result_array();
	    }
	    
	    return $data;
	}
	
	
	public function is_in_cart($items = array()){
	    $this->db->select('shopping_cart_id, quantity');
	    $this->db->where('customer_id',$this->session->userdata('customer_id'));
	    $this->db->where('product_id',$items['product_id']);
	    
	    $result = $this->db->get(self::TBL_SHOPPING_CARTS);
	    
	    foreach ($result->result_array() as $cart){
	        $this->db->where('shopping_cart_id', $cart['shopping_cart_id']);
	        $attr_result = $this->db->get(self::TBL_SHOPPING_CART_ATTRIBUTES);
	        
	        //商品没有属性
	        if($attr_result->num_rows() == 0){
	            if(isset($items['options']) && !empty($items['options'])){
	                return FALSE;
	            }else{
	                return $cart;
	            }
	        }
	        
	        //判断商品属性是否相等
    	    $flag = 0;
    	    $attrs = $attr_result->result_array();
    	    foreach ($items['options'] as $key => $value){
    	        foreach ($attrs as $attr){
    	            if ($attr['option_id'] == $key && $attr['option_value_id'] == $value){
    	                $flag++;
    	                break;
    	            }
    	        }
    	    }
    	    
    	    if ($flag==count($items['options'])){
    	        return $cart;
    	    }
	    }
	    return FALSE;
	}
}

/* End of file shipping_carts.php */
/* Location: ./application/models/sys/shipping_carts.php */